#include "trigger_flags.h"
#include <stdio.h>
#include "led.h"
#include "key.h"

static bool s_bBaseTimerTrigger = false;
static bool s_bRTCTimerTrigger = false;
static bool s_bKeyTrigger = false;
static uint16_t s_uiKeyCode;

static USART_INPUT s_stUsartInput = {0x00};

void USARTReceiveProc(USART_INT_REASON eReason, uint8_t uiReceiveData) {
    if (USART_INT_REASON_IDLE == eReason) {
        s_stUsartInput.bIsTriggered = true;
    } else if (USART_INT_REASON_REV == eReason) {
        if (true == s_stUsartInput.bHalfRev) {
            s_stUsartInput.unKeyValue.uiByte[0] = uiReceiveData;
            s_stUsartInput.bIsTriggered = true;
        } else {
            s_stUsartInput.unKeyValue.uiByte[1] = uiReceiveData;
            s_stUsartInput.bHalfRev = true;
        }
    }
}

void KeyEventProc(uint16_t uiKeyCode, KEY_EVENT eEvent) {
    if (eEvent == KEY_EVENT_DOWN) {
//        if (uiKeyCode!=0x0d)
//            s_uiKeyCode = uiKeyCode|(0x01<<8);
        s_uiKeyCode = uiKeyCode;
        s_bKeyTrigger = true;
    }
}

void TimerInterruptProc(void) {
    s_bBaseTimerTrigger = true;

//	LED1_Reverse();

    Key_Scan();

}

void RTCInterruptProc(uint32_t uiTimeStamp) {
    s_bRTCTimerTrigger = true;
}

bool BaseTimerIsTrigger(void) {
    return s_bBaseTimerTrigger;
}

void BaseTimerTriggerReset(void) {
    s_bBaseTimerTrigger = false;
}

bool UsartIsReceived(void) {
    return s_stUsartInput.bIsTriggered;
}

bool KeyIsReceived(void) {

    return s_bKeyTrigger;
}

uint16_t GetReceivedCode(void) {
    return s_stUsartInput.unKeyValue.uiKeyCode;
}

uint16_t GetKeyCode(void) {
    return s_uiKeyCode;
}

void UsartTriggerReset(void) {
    s_stUsartInput.bIsTriggered = false;
    s_stUsartInput.bHalfRev = false;
    s_stUsartInput.unKeyValue.uiKeyCode = 0;
}

void KeyTriggerReset(void) {
    s_bKeyTrigger = false;
    s_uiKeyCode = 0;
}

bool RTCTimerIsTrigger(void) {
    return s_bRTCTimerTrigger;
}

void RTCTimerTriggerReset(void) {
    s_bRTCTimerTrigger = false;
}

